package com.jeecms.wicool.action.share;

import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.image.ShareImageEntity;
import com.jeecms.wicool.entity.share.FoodShare;
import com.jeecms.wicool.entity.share.Praise;
import com.jeecms.wicool.manager.image.ShareImageEntityManager;
import com.jeecms.wicool.manager.share.FoodShareMng;
import com.jeecms.wicool.manager.share.PraiseMng;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;

import static com.jeecms.common.page.SimplePage.cpn;

/**
 * Created by cuiyanlei on 15/6/17.
 */
@Controller
@RequestMapping("/share")
public class FoodShareAct {

    @RequestMapping("v_list.do")
    public String list(String title, String content, Integer pageNo, HttpServletRequest request, ModelMap model) {
        Pagination pagination = manager.getPage(title, content, cpn(pageNo),
                CookieUtils.getPageSize(request));
        List<FoodShare> shares = (List<FoodShare>) pagination.getList();
        for (FoodShare share : shares) {
            Praise praise = praiseMng.findById(share.getId(), Praise.FOOD_SHARE);
            if (praise == null) {
                praise = new Praise();
                praise.setCount(0);
            }
            share.setPraise(praise);
        }
        model.addAttribute("pagination", pagination);
        model.addAttribute("shares", shares);
        model.addAttribute("title", title);
        model.addAttribute("content", content);
        return "share/food_share_list";
    }

    @RequestMapping("v_edit.do")
    public String edit(Integer id, ModelMap model) {
        FoodShare bean = manager.findById(id);
        List<ShareImageEntity> images = imgMng.getAll(bean.getId(), ShareImageEntity.SHARE_IMG);
        model.addAttribute("foodShare", bean);
        model.addAttribute("images", images);
        model.addAttribute("foodShareId", id);
        return "share/food_share_edit";
    }

    @RequestMapping("o_update.do")
    public String update(Integer id, String title, String subTitle, String content, String[] imgUrls, ModelMap model) {
        FoodShare bean = manager.findById(id);
        bean.setTitle(title);
        bean.setSubTitle(subTitle);
        bean.setContent(content);
        imgMng.deleteByForeginKey(id, ShareImageEntity.SHARE_IMG);
        for (String imgUrl : imgUrls) {
            ShareImageEntity img = new ShareImageEntity(null, id, imgUrl, 0, ShareImageEntity.SHARE_IMG);
            imgMng.save(img);
        }
        bean.setUpdateTime(new Timestamp(DateTime.now().getMillis()));
        manager.updateByUpdater(new Updater<FoodShare>(bean));
        model.addAttribute("message", "global.success");
        return edit(id, model);
    }


    @RequestMapping("o_changeStatus.do")
    public String validate(Integer id, Integer status) {
        FoodShare bean = manager.findById(id);
        bean.setStatus(status.shortValue());
        manager.updateByUpdater(new Updater<FoodShare>(bean));
        return "redirect:v_list.do";
    }

    @RequestMapping("o_delete.do")
    public String delete(Integer[] ids) {
        for (Integer id : ids) {
            FoodShare bean = manager.findById(id);
            praiseMng.deleteByForeginKey(bean.getId(), Praise.FOOD_SHARE);
            imgMng.deleteByForeginKey(id, ShareImageEntity.SHARE_IMG);
            manager.deleteById(bean.getId());
        }
        return "redirect:v_list.do";
    }

    @Autowired
    private FoodShareMng manager;

    @Autowired
    private PraiseMng praiseMng;

    @Autowired
    private ShareImageEntityManager imgMng;
}
